{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Installation and deployment of 5G RAN (gNB) of Open Air Interface (OAI)\n",
    "## Objectives\n",
    "- Install and configure the OAI gNB \n",
    "- Deploy a gNB together a previous configured 5GC\n",
    "- Inspec gNB status in 5GC Core"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "At the end of a set of three hands-on (this is the second one), the goal is to run the simulated OAI scenario, which consists of the 5G core (5GC), gNB in CU/DU split mode, and user equipment (UE), according to the image below: \n",
    "\n",
    "![5GC_01](./FIGS/H01_5GCore_UNI_III/5GC_deploy.jpg)\n",
    "\n",
    "Now, we are going to install, configure, deploy and interact to gNB with CU/DU split mode. \n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## gNB Installation\n",
    "\n",
    "To install OAI RAN 5G, go back to /home/dco1020 and follow this tutorial.\n",
    "\n",
    "**Step 0:** go back to /home/dco1020. In a Linux Terminal, type:\n",
    "\n",
    "sudo su\n",
    "\n",
    "cd /home/dco1020\n",
    "\n",
    "\n",
    "**Step 1:** Install dependencies\n",
    "\n",
    "sudo add-apt-repository ppa:ettusresearch/uhd\n",
    "\n",
    "sudo apt-get install libuhd-dev uhd-host -y\n",
    "\n",
    "sudo apt-get install libuhd4.2.0 -y\n",
    "\n",
    "sudo apt-get install libuhd4.4.0 -y\n",
    "\n",
    "**Step 2:** Fix problem in the libuhd. When you run the commands below, if you get the error \"cannot access archive\", you can continue to the next step.\n",
    "\n",
    "sudo dpkg -i --force-overwrite /var/cache/apt/archives/libuhd4.2.0_4.2.0.1-0ubuntu1~focal1_amd64.deb\n",
    "\n",
    "sudo dpkg -i --force-overwrite /var/cache/apt/archives/libuhd4.4.0_4.4.0.0-0ubuntu1~focal1_amd64.deb\n",
    "\n",
    "**Step 3:** Clone OpenAirInterface 5G RAN repository\n",
    "\n",
    "git clone https://gitlab.eurecom.fr/oai/openairinterface5g.git\n",
    "\n",
    "cd /home/dco1020/openairinterface5g\n",
    "\n",
    "git checkout 2023.w47\n",
    "\n",
    "**Step 4:** Run the installation script\n",
    "\n",
    "source oaienv\n",
    "\n",
    "cd cmake_targets\n",
    "\n",
    "sudo ./build_oai -I \n",
    "\n",
    "sudo ./build_oai -I --install-optional-packages\n",
    "\n",
    "sudo ./build_oai --gNB --eNB --UE --nrUE \n",
    "\n",
    "**Attention**: you may find Wireshark configuration screen, select \"yes\" to continue.\n",
    "\n",
    "**Attention**: be patient, the last command could take a whileeeee.\n",
    "\n",
    "If you have any compilation error, please, try:\n",
    "\n",
    "./build_oai -c -C\n",
    "\n",
    "./build_oai -I\n",
    "\n",
    "sudo ./build_oai -I --install-optional-packages \n",
    "\n",
    "sudo ./build_oai --gNB --eNB --UE --nrUE\n",
    "\n",
    "Take care about conflits between our VM and some windows configuration, If you have compilation problem, please, check and disable WSL, hipervison and virtual machine platforms, acoording to the following figures.\n",
    "\n",
    "\n",
    "![gNB_problem1](./FIGS/H02_5GRAN_UNI_III/gNB_problem1.png)\n",
    "\n",
    "![gNB_problem2](./FIGS/H02_5GRAN_UNI_III/gNB_problem2.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## gNB Configuration\n",
    "\n",
    "In a Linux Terminal, follow the steps below:\n",
    "\n",
    "**Step 1:** Configure the PLMN and TAC values\n",
    "\n",
    "The Public Land Mobile Network (PLMN) and Tracking Area Code (TAC) will be configured in the CU (/home/dco1020/openairinterface5g/targets/PROJECTS/GENERIC-NR-5GC/CONF/cu_gnb.conf) and DU (/home/dco1020/openairinterface5g/targets/PROJECTS/GENERIC-NR-5GC/CONF/du_gnb.conf) configuration files. \n",
    "\n",
    "Figure below shows the part of the code that must be modified in the CU and DU files (lines 17 and 18). The parameters shown in figure should be modified according to the values present in docker compose in the \"oai-amf\" session (/home/dco1020/oai-cn5g-fed/docker-compose/docker-compose-basic-nrf.yaml). \n",
    "\n",
    "![gNB_config_01](./FIGS/H02_5GRAN_UNI_III/gNB_config_01.png)\n",
    "\n",
    "\n",
    "Figure below shows the parameters that should be checked and passed to the CU and DU configuration files: PLMN_SUPPORT_TAC (referring to \"tracking_area_code\"), PLMN_SUPPORT_MCC (referring to \"mcc\"), PLMN_SUPPORT_MNC (referring to \"mnc\"), SST_0 (referring to \"sst\") and the SD_0 (referring to \"sd\"). If the \"sd\" parameter is not in the CU and DU configuration files, simply add it according to line 18 in previous figure.\n",
    "\n",
    "![gNB_config_02](./FIGS/H02_5GRAN_UNI_III/gNB_config_02.png)\n",
    "\n",
    "\n",
    "Open files by typing:\n",
    "\n",
    "gedit /home/dco1020/openairinterface5g/targets/PROJECTS/GENERIC-NR-5GC/CONF/cu_gnb.conf\n",
    "\n",
    "gedit /home/dco1020/openairinterface5g/targets/PROJECTS/GENERIC-NR-5GC/CONF/du_gnb.conf\n",
    "\n",
    "gedit /home/dco1020/oai-cn5g-fed/docker-compose/docker-compose-basic-nrf.yaml\n",
    "\n",
    "**Step 2:**  Configure the network interfaces\n",
    "\n",
    "This part you will configure the N2 interface in (/home/dco1020/openairinterface5g/targets/PROJECTS/GENERIC-NR-5GC/CONF/cu_gnb.conf), such as in figure below. This interface is responsible to connect the CU and the AMF.\n",
    "\n",
    "![gNB_config_03](./FIGS/H02_5GRAN_UNI_III/gNB_config_03.png)\n",
    "\n",
    "You need to verify/change the IP addresses. You can find the OAI-AMF IP address inside /home/dco1020/oai-cn5g-fed/docker-compose/docker-compose-basic-nrf.yaml, such as in figure below. You must get this IP address and put it in the \"amf_ip_address\" parameter, in the \"ipv4\" field (such as in previous figure).\n",
    "\n",
    "![gNB_config_04](./FIGS/H02_5GRAN_UNI_III/gNB_config_04.png)\n",
    "\n",
    "\n",
    "Check if the IP Address of cu_gnb.conf is the same of docker-compose-basic-nrf.yaml. To edit cu_gnb.conf, type:\n",
    "\n",
    "gedit /home/dco1020/openairinterface5g/targets/PROJECTS/GENERIC-NR-5GC/CONF/cu_gnb.conf\n",
    "\n",
    "\n",
    "**Step 3:** to check a virtual interface (demo-oai), type:\n",
    "\n",
    "ifconfig -a\n",
    "\n",
    "You should find a virtual interface (demo-oai) (as shown in figure below). This interface serves to connect the CU with OAI-AMF.  You must get its IP address and name to configure the N2 interface such as two figures before. \n",
    "\n",
    "![gNB_config_05](./FIGS/H02_5GRAN_UNI_III/gNB_config_05.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Deployment\n",
    "\n",
    "The deployment of the CU and DU is done separately, and the CU needs to be started before the DU. \n",
    "\n",
    "**NOTE:** The CU and DU will be running on two different terminals\n",
    "\n",
    "**Step 1:** open a new terminal using crtl+Alt+T and type:\n",
    "\n",
    "sudo su\n",
    "\n",
    "cd /home/dco1020/openairinterface5g/cmake_targets/ran_build/build/\n",
    "\n",
    "sudo RFSIMULATOR=server ./nr-softmodem --rfsim --sa -O ../../../targets/PROJECTS/GENERIC-NR-5GC/CONF/cu_gnb.conf --continuous-tx \n",
    "\n",
    "**Mudança 2024.1:** se você tiver problema com o comando acima, tente:\n",
    "\n",
    "sudo ./nr-softmodem -O ../../../targets/PROJECTS/GENERIC-NR-5GC/CONF/cu_gnb.conf --sa\n",
    "\n",
    "**Step 2:** Open another terminal using crtl+Alt+T and type: \n",
    "\n",
    "sudo su \n",
    "\n",
    "cd /home/dco1020/openairinterface5g/cmake_targets/ran_build/build/\n",
    "\n",
    "sudo RFSIMULATOR=server ./nr-softmodem --rfsim --sa -O ../../../targets/PROJECTS/GENERIC-NR-5GC/CONF/du_gnb.conf --continuous-tx --gNBs.[0].min_rxtxtime 6\n",
    "\n",
    "**Mudança 2024.1:** se você tiver problema com o comando acima, tente:\n",
    "\n",
    "sudo ./nr-softmodem -O ../../../targets/PROJECTS/GENERIC-NR-5GC/CONF/du_gnb.conf --rfsim --sa -E\n",
    "\n",
    "**Step 3**: To check the connection of gNodeB to the Core, see AMF logs. Open another terminal (the third one) using crtl+Alt+T and type: \n",
    "\n",
    "docker logs -f oai-amf\n",
    "\n",
    "The expected return is shown in figure below, where gNodeB shows the connected status.\n",
    "\n",
    "![gNB_config_06](./FIGS/H02_5GRAN_UNI_III/gNB_config_06.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Deliverable 02:\n",
    "\n",
    "- Inspec the gNB configuration files and change its name to gNB-DCO1020. Show the new gNB name in the AMF log file."
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
